import { useAtom } from "jotai";
import { atomMap } from "./store";
import { ISelector } from "./types";

export function useSelector<TState = any, TSelected = any>(
  selector: ISelector<TState, TSelected>
): TSelected {
  const models = {} as TState;
  Object.keys(atomMap).forEach((namespace) => {
    const atom = atomMap[namespace];
    const [atomState] = useAtom(atom);
    models[namespace] = atomState;
  });
  return selector(models);
}
